package com.lwl.leetcode;

/**
 * @author lwl
 * @Description TODO
 * @date 2023/7/25 18:11
 */
public class Code07ReverseInt {

    public int reverse(int x) {
        boolean isNegative = x < 0;
        // 统一转为复数处理，因为复数的值域比正数要大
        x = isNegative ? x : -x;
        // 处理res溢出的情况
        int m = Integer.MIN_VALUE / 10;
        int n = Integer.MIN_VALUE % 10;
        int res = 0;
        while (x != 0) {
            if (res < m || (res == m && x % 10 < n)) {
                return 0;
            }
            res = res * 10 + x % 10;
            x /= 10;
        }
        return isNegative ? res : Math.abs(res);
    }

}
